#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int f[N], d[N], sz[N];
void init(int n) { rep(i, 1, n) f[i] = i, d[i] = 0, sz[i] = 1; }
int find(int x) {
  if (f[x] == x) return x;
  int rt = find(f[x]);
  d[x] += d[f[x]];
  return f[x] = rt;
}
void merge(int x, int y) {  // x...y
  int fx = find(x), fy = find(y);
  if (fx == fy) return;
  f[fy] = fx;
  d[fy] = sz[fx];
  sz[fx] += sz[fy];
}
int query(int x, int y) {
  if (x == y) return 0;
  int fx = find(x), fy = find(y);
  if (fx != fy) return -1;
  return abs(d[x] - d[y]) - 1;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init(30000);
  int t;
  cin >> t;
  while (t--) {
    char op;
    int x, y;
    cin >> op >> x >> y;
    if (op == 'M')
      merge(x, y);
    else
      cout << query(x, y) << endl;
  }
  return 0;
}